package com.engine.qz.controller;

import com.engine.common.util.ParamUtil;
import com.engine.qz.util.response.ReturnResult;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.qz.job.kqCustomCornJob;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/3/19
 * @Description
 * @Author AdminZm
 */
public class CommonController {

    BaseBean baseBean = new BaseBean();

    @POST
    @Path("/putrytx")
    @Produces(MediaType.APPLICATION_JSON)
    public String putRytx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        baseBean.writeLog("putRytx start.");
        Map<String, Object> resultDatas = new HashMap<>();
        try {
            Map<String, Object> params = ParamUtil.request2Map(request);
            baseBean.writeLog("putRytx params:" + params);
            String ry = Util.null2String(params.get("ry"));
            String qqId = Util.null2String(params.get("qqid"));
            if (StringUtils.isNotEmpty(qqId)) {
                RecordSet rs = new RecordSet();
                String tableName = null;
                rs.execute("SELECT \n" +
                        "    b.tablename \n" +
                        "FROM \n" +
                        "    workflow_requestbase a\n" +
                        "    JOIN workflow_base c ON a.workflowid = c.id\n" +
                        "    JOIN workflow_bill b ON c.formid = b.id\n" +
                        "WHERE \n" +
                        "    a.requestid = " + qqId);
                if (rs.next()) {
                    tableName = rs.getString("tablename");
                }
                if (StringUtils.isEmpty(tableName)) {
                    return new Gson().toJson(resultDatas);
                }
                if (StringUtils.isNotEmpty(ry)) {
                    String zpId = null;
                    rs.execute("SELECT zp FROM uf_csfjb WHERE ry = " + ry);
                    if (rs.next()) {
                        zpId = rs.getString("zp");
                    }
                    if (StringUtils.isNotEmpty(zpId)) {
                        rs.execute("UPDATE " + tableName + " SET zp = '" + zpId + "' WHERE requestid = " + qqId);
                    } else {
                        rs.execute("UPDATE " + tableName + " SET zp = null WHERE requestid = " + qqId);
                    }
                } else {
                    rs.execute("UPDATE " + tableName + " SET zp = null WHERE requestid = " + qqId);
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("putRytx error:" + e);
        }
        return new Gson().toJson(resultDatas);
    }

    @GET
    @Path("/pullstatickq")
    @Produces(MediaType.APPLICATION_JSON)
    public String pullstatickq(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> params = ParamUtil.request2Map(request);
        kqCustomCornJob kqCustomCornJob = new kqCustomCornJob();
        kqCustomCornJob.setMonth((String) params.get("month"));
        return "succes";
    }
}
